package com.yimu.lib.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import com.yimu.lib.Receiver.AndroidNotify;
import com.yimu.lib.Receiver.AppNotify;
import com.yimu.lib.http.HttpUtils;
import com.yimu.lib.util.MyLogger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 描述： BaseApplication
 * 创建者：  Churck
 * 创建时间：16/6/8 17:03
 * 修改者： Churck
 * 修改时间：16/6/8 17:03
 * 版 本：v1.0
 */
public  class  BaseApplication extends Application {
    protected boolean isdebug=true;
    // 存储Activity的容器
    private List<Activity> activityList = new ArrayList<>();
    // PAApplication单例对象
    private static BaseApplication instance;
    @Override
    public void onCreate() {
        super.onCreate();
        HttpUtils.getInstance().init();
        instance = this;
        MyLogger.isDebug=isdebug;
        AppNotify.setNotifyHandler(new AndroidNotify());


    }


    public static BaseApplication getInstance() {
        return instance;
    }
    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    /**
     * 根据类名销栈
     *
     * @param activity
     */
    public void removeActivity(Class<? extends Activity> activity) {
        Iterator<Activity> iterator = activityList.iterator();
        while (iterator.hasNext()) {
            Activity iterator_activity = iterator.next();
            if (iterator_activity.getClass() == activity) {
                iterator.remove();
                iterator_activity.finish();
            }
        }
    }
    private static void loadClass(Class<?> clazz) {
        try {
            Class.forName(clazz.getName());
        } catch (ClassNotFoundException e) {
        }
    }
    // 从容器中删除Activity
    public void removeActivity(Activity activity) {
        if (null != activity && activityList.contains(activity)) {
            activityList.remove(activity);
        }
    }

    // 清除所有的 Activity
    public void clearActivities() {
        for (Activity activity : activityList) {
            activity.finish();
        }
        activityList.clear();
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }
}
